package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> list) {
         if(list!=null && list.size()>0){
             for (OrderSetting orderSetting : list) {
                 Date orderDate = orderSetting.getOrderDate();
                 long count = orderSettingDao.findCountByOrderDate(orderDate);
                 if(count>0){
                     orderSettingDao.editNumberByOrderDate(orderSetting);
                 }else {
                     orderSettingDao.add(orderSetting);
                 }

             }
         }

    }

    @Override
    public List<Map> getOrderSettingByOrderMonth(String date) {
        String begin = date+"-1";
        System.out.println(begin);
        String end = date+"-31";
        System.out.println(end);
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByOrderMonth(map);

        List<Map> mapList = new ArrayList<>();
        if(list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> m = new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                mapList.add(m);
            }
        }

        return mapList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        Date orderDate = orderSetting.getOrderDate();
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if(count>0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
